package com.blb.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 循环栏栅
 */
public class CyclicBarrierDemo {

    public static void main(String[] args) {
        //创建栏栅对象
        CyclicBarrier barrier = new CyclicBarrier(3,() -> {
            System.out.println("发令枪响了！~！！");
        });
//        for (int j = 0; j < 2; j++) {
//            //重置数量
//            barrier.reset();
            for (int i = 0; i < 3; i++) {
                new Thread(() -> {
                    System.out.println(Thread.currentThread().getName() + "准备好了！！");
                    try {
                        Thread.sleep(2000);
                        //开始等待, parties减1，到0就全部唤醒执行
                        barrier.await();
                        System.out.println(Thread.currentThread().getName() + "冲啊！！");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
//        }
    }
}
